## Make the latex table for coefplot_anyDA_new.pdf and an appendix table.

library(here)
library(xtable)
load(here("Analysis", "analysis_anyDA_new.rda"), verbose = TRUE)

## Add captions and explanation
tab1 <- xtable(res, digits = 3)

print(tab1, type = "latex", file = here("Figures_Tables", "coefplot_table_anyDA_new.tex"), booktabs = TRUE)

## Table in the Appendix comparing estimation methods
colnames(appendix_tab) <- c("MLM", "Bayes MLM", "FE", "MLM", "Bayes MLM", "FE")
rownames(appendix_tab) <- c("Estimate", "Standard Error", "95% CI Low", "95% CI High")
appendix_xtab <- xtable(appendix_tab,
    caption = "Comparison of results from multilevel
    models (MLM and Bayes MLM) (with random effects for both dissemination area and matched
    pair) and Fixed Effects models (FE) (with fixed-effects for matched
    pair and HC2 standard errors \\autocite{gerber2012field}). The Bayesian MLMs use the default priors of the \\lstinline{brm} command from the \\lstinline{brms} package for R: students t priors  ",
    label = "tab:app_mlm_vs_fe", digits = 4
)
appendix_xtab

addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- -1
addtorow$command <- c(" & \\multicolumn{3}{c}{Social Cohesion} & \\multicolumn{3}{c}{Collective Efficacy} \\\\\n")

print.xtable(appendix_xtab,
    align = "lcccc",
    add.to.row = addtorow,
    file = here("Figures_Tables", "appendix_xtab_anyDA_new.tex"), type = "latex", booktabs = TRUE,
    table.placement = "H"
)
